<script>
  $(function() {
    $( "#ngaysinh" ).datepicker({
    	showOn: "button",
      changeMonth: true,
      changeYear: true,
     
      buttonImageOnly: true
    });
  });
  </script>
<?php
require 'db.php';

$malop = isset ( $_POST ['malop'] ) ? $_POST ['malop'] : "";
$valid = true;
$error = array (
		'mssv' => '',
		'holot' => '',
		'ten' => '',
		'gioitinh' => '',
		'matkhau' => '',
		'email' => '',
		'quequan' => '',
		'insert' => '',
		'ngaysinh'=>''
);
$isPost = (count ( $_POST ) > 0);
$mssv = "";
$holot = '';
$ten = '';
$gioitinh = '';
$matkhau = '';
$email = '';
$quequan = '';
$ngaysinh='';
if ($isPost) {
	$malop = isset ( $_POST ['malop'] ) ? $_POST ['malop'] : "";
	if (! $malop) {
		die ( 'Khong tim thay ma lop' );
	}
	$mssv = $_POST ['mssv'];
	if (! $mssv) {
		$valid = false;
		$error ['mssv'] = "Chua nhap MSSV.";
	} else {
		$sql = "Select count(*) from dbo_sinhvien ";
		$sql .= " where MaSV =?";
		$stmt = $db->prepare ( $sql );
		$stmt->bindParam ( 1, $mssv );
		$stmt->execute();
		$row = $stmt->fetch ();
		$count = $row [0];
		if ($count) {
			$valid = false;
			$error ['mssv'] = "Ma so sv duoc su dung.";
		}
	}
	$holot = trim ( $_POST ['holot'] );
	if (! (strlen ( $holot ) >= 4 && strlen ( $holot ) <= 50)) {
		$valid = false;
		$error ['holot'] = 'Toi thieu 4 ki tu toi da 50 ki tu';
	}
	$ten = trim ( $_POST ['ten'] );
	if (! (strlen ( $ten ) >= 4 && strlen ( $ten ) <= 50)) {
		$valid = false;
		$error ['ten'] = 'Toi thieu 4 ki tu toi da 50 ki tu';
	}
	$ngaysinh = new DateTime ();
	$ngaysinh = $ngaysinh->createFromFormat ( "d-m-Y", trim ( $_POST ['ngaysinh'] ) );
	$date = date ( "d-m-Y", time () );
	$today = new DateTime ();
	$today->createFromFormat ( "d-m-Y", $date );
	if ($ngaysinh >= $today) {
		$valid = false;
		$error ['ngaysinh'] = 'Ngay sinh khong hop le';
	}
	
	$gioitinh = $_POST['gioitinh'];
	if (($gioitinh == - 1) || ($gioitinh == "")) {
		$valid = false;
		$error ['gioitinh'] = 'Chua chon gioi tinh';
	}
	$email = trim ( $_POST ['email'] );
	$patten = '/@student.ctu.edu.vn$/i';
	if(!preg_match($patten, $email)){
	$valid=false;	
	$error ['email'] = 'Email khong co dang @student.ctu.edu.vn';
	}
	$matkhau = trim ( $_POST ['matkhau'] );
	if (! (strlen ( $matkhau ) >= 4 && strlen ( $matkhau ) <= 50)) {
		$valid = false;
		$error ['matkhau'] = 'Mat khau qua ngan hoac qua dai';
	}
	$quequan = trim ( $_POST ['quequan'] );
	if ( strlen ( $quequan )==0) {
		$valid = false;
		$error ['quequan'] = 'Que quan khong duoc bo trong';
	}
	if($valid){
		$ngaysinh="1993-4-12";
		
		//$ngaysinh=$ngaysinh->format("y-m-d");
		$sql="INSERT INTO dbo_sinhvien( MaSV, Malop, Holot, Ten, Ngaysinh, GioiTinh, MatKhau, Email, QueQuan ) ";
		$sql.="VALUES(?,?,?,?,?,?,?,?,?)";
		$stmt = $db->prepare ( $sql );
		$stmt->bindParam ( 1, $mssv );
		$stmt->bindParam ( 2, $malop );
		$stmt->bindParam ( 3, $holot );
		$stmt->bindParam ( 4, $ten );
		$stmt->bindParam ( 5, $ngaysinh );
		$stmt->bindParam ( 6, $gioitinh );
		$stmt->bindParam ( 7, $matkhau );
		$stmt->bindParam ( 8, $email );
		$stmt->bindParam ( 9, $quequan );
		$stmt->execute ();
		$success=$stmt->rowCount();
		if(!$success){
			
			$error['insert']="Co loi xay ra vui long thu lai sau";
		}else{
			echo "OK";
			exit;
		}
	}
}

?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method ="post" role="form" id="frmSVAdd">
	<div class="form-group">
		<label for="mssv">MSSV</label> <input type="text" name="mssv"
			id="mssv" class="form-control" placeholder="Nhập MSSV"
			value="<?php echo $mssv;?>" />
<?php if($error['mssv']):?>
<label class="alert alert-danger"><?php echo $error['mssv'];?></label>
<?php endif;?>
</div>
	<div class="form-group">
		<label for="holot">Họ tên</label> <input type="text" name="holot"
			id="holot" class="form-control" placeholder="Họ lót"
			value="<?php echo $holot;?>">
<?php if($error['holot']):?>
<label class="alert alert-danger"><?php  echo $error['holot'];?></label>
<?php endif;?>
</div>

	<div class="form-group">
		<label for="ten">Tên</label> <input type="text" name="ten" id="ten"
			class="form-control" placeholder="Tên" value="<?php echo $ten;?>" />
<?php if($error['ten']):?>
<label class="alert alert-danger"><?php  echo $error['ten'];?></label>
<?php endif;?>
</div>
	<div class="form-group">
		<label for="ngaysinh">Ngày sinh</label> <input type="text" name="ngaysinh"
			id="ngaysinh" class="form-control" placeholder="dd-mm-yyyy"
			value="<?php echo $ngaysinh;?>" />
<?php if($error['ngaysinh']):?>
<label class="alert alert-danger"><?php  echo $error['ngaysinh'];?></label>
<?php endif;?>
</div>
	<div class="form-group">
		<label for="gioitinh">Giới tính</label> <select name="gioitinh"
			class="form-control">
			<option value="-1">Chọn</option>
			<option value="0"<?php  echo $gioitinh==0?"selected='selected' ":""?>>Nữ</option>
			<option value="1"<?php  echo $gioitinh==1?"selected='selected' ":""?>>Nam</option>
			</select>
<?php if($error['gioitinh']):?>
<label class="alert alert-danger"><?php  echo $error['gioitinh'];?></label>
<?php endif;?>

	</div>
	<div class="form-group">
		<label for="matkhau">Mật khẩu</label> <input type="text" name="matkhau"
			id="matkhau" class="form-control" placeholder="Mật khẩu"
			value="<?php echo $matkhau;?>" />
<?php if($error['matkhau']):?>
<label class="alert alert-danger"><?php  echo $error['matkhau'];?></label>
<?php endif;?>
		<div class="form-group">
			<label for="email">Email</label> <input type="text"
				name="email" id="email" class="form-control"
				placeholder="@student.ctu.edu.vn" value="<?php echo $email;?>" />
<?php if($error['email']):?>
<label class="alert alert-danger"><?php  echo $error['email'];?></label>
<?php endif;?>
			<div class="form-group">
				<label for="quequan">Quê quán</label>
				<textarea rows="" cols="" name="quequan" id="quequan"
					class="form-control" placeholder="Can tho,hau giang,...">
					 <?php echo $quequan;?></textarea>
<?php if($error['quequan']):?>
<label class="alert alert-danger"><?php  echo $error['quequan'];?></label>
<?php endif;?>
</div>
<?php  if($error['insert']):?>
<label class="alert alert-danger"><?php  echo $error['insert'];?></label>
<?php endif;?>
</form>